DM wurwur debugging


In [2]:
% load_ext autoreload
% autoreload 2

import numpy as np
from random import *
from dmww_classes import *
from plotting_helper import *
from sampling_helper import *


seed(1) # for debugging

Model with toy corpus

Each wurwur (words world model) instance depends on a world, a corpus, and a lexicon - learned by the model - that connects the two.


In [9]:
w = World(n_words=8,n_objs=8)
w.show()
            
c = Corpus(world=w, n_sents=40, n_per_sent=2)
c.show()


n_objs = 8
n_words = 8
o: [2 4] w: [2 4]
o: [4 0] w: [4 0]
o: [2 0] w: [2 0]
o: [2 5] w: [2 5]
o: [5 0] w: [5 0]
o: [0 5] w: [0 5]
o: [0 2] w: [0 2]
o: [2 0] w: [2 0]
o: [0 7] w: [0 7]
o: [3 6] w: [3 6]
o: [5 1] w: [5 1]
o: [5 1] w: [5 1]
o: [4 2] w: [4 2]
o: [7 2] w: [7 2]
o: [6 4] w: [6 4]
o: [6 4] w: [6 4]
o: [6 2] w: [6 2]
o: [5 4] w: [5 4]
o: [4 3] w: [4 3]
o: [4 2] w: [4 2]
o: [7 4] w: [7 4]
o: [4 0] w: [4 0]
o: [4 1] w: [4 1]
o: [1 3] w: [1 3]
o: [4 1] w: [4 1]
o: [2 3] w: [2 3]
o: [3 2] w: [3 2]
o: [0 2] w: [0 2]
o: [5 3] w: [5 3]
o: [4 5] w: [4 5]
o: [5 4] w: [5 4]
o: [0 2] w: [0 2]
o: [5 1] w: [5 1]
o: [7 0] w: [7 0]
o: [7 1] w: [7 1]
o: [6 5] w: [6 5]
o: [2 6] w: [2 6]
o: [1 5] w: [1 5]
o: [3 7] w: [3 7]
o: [0 4] w: [0 4]

Check the lexicon counts via co-occurrence:


In [10]:
l = CoocLexicon(world=w)
l.learn_lex(c)
lexplot(l, w)


Now try this with the Gibbs sampler (the actual model):


In [11]:
p = Params(n_samps=20,
           alpha_r=.1,
           alpha_nr=10,
           empty_intent=.0001,
           n_hypermoves=5)

l = GibbsLexicon(c,p,verbose=0,hyper_inf=True)
l.learn_lex(c, p)
lexplot(l, w, certainwords = 0)
l.ref



...................

[[ 6.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  5.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  8.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  5.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  5.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  4.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  5.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  2.]]
nr: [ 126.   43.  126.   62.  170.  148.   81.   84.]
intent_hp_b: 1.0000
intent_hp_a: 1.0000
empty_intent: 0.0001
alpha_nr_hp: 2.0000
alpha_r_hp: 1.0000
n_samps: 20.0000
alpha_nr: 4.4581
alpha_r: 0.0186
n_hypermoves: 5.0000

 *** average sample time: 0.050 sec
Out[11]:
array([[ 6.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  5.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  8.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  5.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  5.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  4.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  5.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  2.]])

Model with mini "real" corpus


In [12]:
w = World(corpus = 'corpora/corpus_toy.csv')
w.show()

c = Corpus(world=w, corpus = 'corpora/corpus_toy.csv')
c.show()


n_objs = 6
n_words = 14
o: [3 5 0 2 4] w: [ 3  4  2 11  5 10  7]
o: [3 5 0 2 4] w: [13  0  4  1  9  8]
o: [3 5 0 2 1 4] w: [ 3  2 12  6]

In [14]:
l = CoocLexicon(w)
l.learn_lex(c)
l.show()


[[ 1.  1.  2.  2.  2.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
 [ 0.  0.  1.  1.  0.  0.  1.  0.  0.  0.  0.  0.  1.  0.]
 [ 1.  1.  2.  2.  2.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
 [ 1.  1.  2.  2.  2.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
 [ 1.  1.  2.  2.  2.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
 [ 1.  1.  2.  2.  2.  1.  1.  1.  1.  1.  1.  1.  1.  1.]]

In [15]:
l = GibbsLexicon(c, p,
                 verbose=0,
                 hyper_inf=True)

l.learn_lex(c,p)
lexplot(l,w,certainwords = 0)
l.params.show()



...................

[[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  1.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  1.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  1.  0.  0.  0.  0.  0.  0.  0.]]
nr: [ 21.  21.  42.  42.  42.  21.   0.   0.   0.  21.  21.  21.  21.  21.]
intent_hp_b: 1.0000
intent_hp_a: 1.0000
empty_intent: 0.0001
alpha_nr_hp: 2.0000
alpha_r_hp: 1.0000
n_samps: 20.0000
alpha_nr: 10.0000
alpha_r: 0.1000
n_hypermoves: 5.0000

 *** average sample time: 0.023 sec
intent_hp_b: 1.0000
intent_hp_a: 1.0000
empty_intent: 0.0001
alpha_nr_hp: 2.0000
alpha_r_hp: 1.0000
n_samps: 20.0000
alpha_nr: 10.0000
alpha_r: 0.1000
n_hypermoves: 5.0000

Model with full-scale real corpus


In [33]:
corpusfile = 'corpora/corpus.csv'
w = World(corpus=corpusfile)
w.show()

c = Corpus(world=w, corpus=corpusfile)


n_objs = 23
n_words = 420

In [49]:
p = Params(n_samps=100,
           alpha_r=.1,
           alpha_nr=10,
           empty_intent=.0001,
           n_hypermoves=5)

l = GibbsLexicon(c, p,
                 verbose=0,
                 hyper_inf=True)

l.learn_lex(c,p)
lexplot(l,w)



...............................................................................

...................
 *** average sample time: 1.321 sec

In [50]:
for o in range(w.n_objs):
    wd = where(l.ref[o,:] == max(l.ref[o,:]))
    print "o: %s, w: %s" % (w.objs_dict[o][0], w.words_dict[wd[0][0]][0])


o: rattle, w: yeah
o: mirror, w: david
o: pig, w: pig
o: duck, w: how
o: ring, w: it
o: girl, w: daddy
o: lamb, w: lamb
o: book, w: book
o: hat, w: hat
o: bunny, w: bunnyrabbit
o: sheep, w: sheep
o: eyes, w: ready
o: woman, w: theres
o: kitty, w: meow
o: bear, w: bottle
o: hand, w: yeah
o: baby, w: baby
o: dax, w: all
o: man, w: and
o: boy, w: and
o: cow, w: moocow
o: face, w: and
o: bird, w: bigbird

Gold standard comparision

Note that f-score here is assessed via ANY non-zero counts.

We should think about the best way to do this for corpora


In [54]:
corpusfile = 'corpora/gold_standard.csv'
c_gs = Corpus(world=w, corpus = corpusfile)
get_f(l.ref, c_gs)


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-54-cc001aa21c77> in <module>()
      1 corpusfile = 'corpora/gold_standard.csv'
      2 c_gs = Corpus(world=w, corpus = corpusfile)
----> 3 get_f(l.ref, c_gs).round

AttributeError: 'tuple' object has no attribute 'round'

In [ ]: